package com.adcc.ags.cgw.resources;

import com.adcc.ags.cgw.App;
import com.adcc.ags.cgw.configuration.BlackListFactory;
import com.adcc.ags.cgw.configuration.UserListFactory;
import com.adcc.ags.cgw.entity.user.BlackListUser;
import com.adcc.ags.cgw.entity.user.GWMode;
import com.adcc.ags.cgw.entity.user.User;
import com.adcc.ags.cgw.proxy.UserProxy;
import com.adcc.ags.cgw.utils.Constant;
import com.adcc.ags.cgw.utils.FileUtil;
import com.adcc.ags.cgw.vo.ResultState;
import com.adcc.ags.cgw.vo.ResultVO;
import com.adcc.utility.log.Log;
import com.adcc.utility.xml.ADCCXML;
import com.adcc.utility.xml.ADCCXMLFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Optional;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.File;
import java.util.List;

/**
 * 网关接口
 */
@Path("/gw")
@Produces("application/json;charset=UTF-8")
public class GWResource {

    /**
     * 加载用户配置
     * @return
     * @throws JsonProcessingException
     */
    @GET
    @Path("/loadUserList")
    public Optional<String> loadUserList() throws JsonProcessingException {
        ResultVO resultVO = new ResultVO();
        try {

            // 加载用户配置
            if(UserProxy.getInstance().getGWMode() != GWMode.EMERGENCY){

                // 加载用户列表
                UserListFactory.getInstance().load();

                // 更新网关用户代理
                UserProxy.getInstance().loadUserList();
            }else{
                resultVO.setResult(ResultState.FAILURE, -1,"load user list failed GW is emergency mode");
            }
            return Optional.of(resultVO.toJSON());
        } catch (Exception ex) {
            Log.error(GWResource.class.getName(), "openEmergencyMode() error", ex);
            resultVO.setResult(ResultState.FAILURE,-1,ex.fillInStackTrace().toString());
            return Optional.of(resultVO.toJSON());
        }
    }

    /**
     * 启动应急模式
     * @return
     * @throws JsonProcessingException
     */
    @GET
    @Path("/startEmergencyMode")
    public Optional<String> startEmergencyMode() throws JsonProcessingException {
        ResultVO resultVO = new ResultVO();
        try {
            if(UserProxy.getInstance().getGWMode() != GWMode.EMERGENCY){

                // 合并黑名单用户列表
                String blackListFile = App.confPath + Constant.STRING_SYNC_USERLIST_DIR + File.separator + Constant.STRING_BLACKLIST_FILE;
                if(FileUtil.isExist(blackListFile)) {
                    ADCCXML adccxml = ADCCXMLFactory.getInstance().createADCCXML();
                    adccxml.parseXMLFile(blackListFile);
                    List<BlackListUser> list = BlackListFactory.getInstance().parseBlackList(adccxml,true);
                    if(list != null && list.size() > 0){
                        for(BlackListUser user : list){
                            BlackListFactory.getInstance().addBlackListUser(user);
                        }
                    }
                    Log.info(GWResource.class.getName(), "merge black user list success");
                }

                // 合并网关用户列表
                String userListFile =  App.confPath + Constant.STRING_SYNC_USERLIST_DIR + File.separator + Constant.STRING_USERLIST_FILE;
                if(FileUtil.isExist(userListFile)) {
                    ADCCXML adccxml = ADCCXMLFactory.getInstance().createADCCXML();
                    adccxml.parseXMLFile(userListFile);
                    List<User> list  = UserListFactory.getInstance().parseUserList(adccxml,true);
                    if(list != null && list.size() > 0){
                        for(User user : list){
                            UserListFactory.getInstance().addUser(user);
                        }
                    }
                    Log.info(GWResource.class.getName(), "merge user list success");
                }

                // 更新网关用户代理
                UserProxy.getInstance().setGWMode(GWMode.EMERGENCY);
                UserProxy.getInstance().loadUserList();
            }
            return Optional.of(resultVO.toJSON());
        } catch (Exception ex) {
            Log.error(GWResource.class.getName(), "startEmergencyMode() error", ex);
            resultVO.setResult(ResultState.FAILURE, -1, ex.fillInStackTrace().toString());
            return Optional.of(resultVO.toJSON());
        }
    }

    /**
     * 关闭应急模式
     * @return
     * @throws JsonProcessingException
     */
    @GET
    @Path("/stopEmergencyMode")
    public Optional<String> stopEmergencyMode() throws JsonProcessingException {
        ResultVO resultVO = new ResultVO();
        try{
            if(UserProxy.getInstance().getGWMode() == GWMode.EMERGENCY){
                // 重新加载网关用户列表
                UserListFactory.getInstance().load();
                BlackListFactory.getInstance().load();

                // 更新网关用户代理
                UserProxy.getInstance().setGWMode(GWMode.NORMAL);
                UserProxy.getInstance().loadUserList();
            }
            return Optional.of(resultVO.toJSON());
        }catch (Exception ex){
            Log.error(GWResource.class.getName(), "stopEmergencyMode() error", ex);
            resultVO.setResult(ResultState.FAILURE,-1,ex.fillInStackTrace().toString());
            return Optional.of(resultVO.toJSON());
        }
    }
}
